﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BanXeTang
{
    class ManChoi
    {
        public List<VatPham> _vatPham = new List<VatPham>();//danh sách các vật phẩm trong màn chơi
        public int soTankCanDiet;//Số xe tank địch còn cần phải diệt trong màn chơi
        public int _nGameEntity;
        public List<VisibleGameEntity> _gameEntity;

        public int _nMap;
        public List<Map> _map;

        public int _nCanCu;
        public float[,] maTranCanCu;//Ma trận lưu tọa độ của các căn cứ địch

        public float [,] maTranTank;//Ma trận lưu tọa độ của các xe tank đich trong màn chơi
        public float[,] maTranVatCan;// Ma trận lưu tọa độ của các vật cản trong màn chơi
        public int _nVatCan;
        public int _nTankDich;
        public List<TankDich> _tankDich = new List<TankDich>();
        public List<MySprite> _vatCan = new List<MySprite>();
        public List<MySprite> _canCu = new List<MySprite>();

        public int _maxTankDich;


        public ManChoi(MapManager mapManager, CharacterManager characterManager, int i)
        {
            _nMap = 1;
            _map = new List<Map>();
            _map.Add(new Map());
            _map[0] = (Map)mapManager.CreateObject(0);
            _map[0].X = 0;
            _map[0].Y = 0;

            DocfileTankDich(@"Content\TankDich" + i.ToString("00") + ".txt");
            KhoitaoTankDich(characterManager);
            DocFileVatCan(@"Content\VatCan" + i.ToString("00") + ".txt");
            KhoitaoVatCan();
            DocFileCanCu(@"Content\CanCu" + i.ToString("00") + ".txt");
            KhoitaoCanCu();

            soTankCanDiet = _maxTankDich;
            _nGameEntity = _nMap + _nTankDich + 1;
            _gameEntity = new List<VisibleGameEntity>();
            for (int j = 0; j < _nMap; j++)
                _gameEntity.Add(_map[j]);
            _gameEntity.Add(Game1.tankTa);
            for (int j = 0; j < _nTankDich; j++)
                _gameEntity.Add(_tankDich[j]);

        }

        public void DocfileTankDich(string filePath)
        {
            StreamReader doc = new StreamReader(filePath);
            char[] spliter = { };
            string Line = doc.ReadLine();
            string[] G = Line.Split();
            _nTankDich = int.Parse(G[0]);
            _maxTankDich = int.Parse(G[1]);
            maTranTank = new float[_nTankDich, 3];
            for (int i = 0; i < _nTankDich; i++)
            {
                Line = doc.ReadLine();
                G = Line.Split(spliter);

                for (int j = 0; j < 3; j++)
                {
                    maTranTank[i, j] = float.Parse(G[j]);
                }
            }
        }
        public void KhoitaoTankDich(CharacterManager characterManager)
        {
            for (int i = 0; i < _nTankDich; i++)
            {
                if (maTranTank[i, 0] == 1)
                {
                    _tankDich.Add(new TankDichLoai1());
                    _tankDich[i] = (TankDichLoai1)characterManager.CreateObject(2);
                }
                else
                {
                    _tankDich.Add(new TankDichLoai2());
                    _tankDich[i] = (TankDichLoai2) characterManager.CreateObject(3);
                }
                _tankDich[i].X = maTranTank[i, 1];
                _tankDich[i].Y = maTranTank[i, 2];
            }
        }

        public void DocFileVatCan(string filePath)
        {
            StreamReader doc = new StreamReader(filePath);
            char[] spliter = { };
            string Line = doc.ReadLine();
            string[] G = Line.Split();
            _nVatCan = int.Parse(G[0]);

            maTranVatCan = new float[_nVatCan, 3];
          
            for (int i = 0; i < _nVatCan; i++)
            {
                Line = doc.ReadLine();
                G = Line.Split(spliter);

                for (int j = 0; j < 3; j++)
                {
                    maTranVatCan[i, j] = float.Parse(G[j]);
                }
            }
        }

        public void DocFileCanCu(string filePath)
        {
            StreamReader doc = new StreamReader(filePath);
            char[] spliter = { };
            string Line = doc.ReadLine();
            string[] G = Line.Split();
            _nCanCu = int.Parse(G[0]);

            maTranCanCu = new float[_nCanCu, 2];

            for (int i = 0; i < _nCanCu; i++)
            {
                Line = doc.ReadLine();
                G = Line.Split(spliter);

                for (int j = 0; j < 2; j++)
                {
                    maTranCanCu[i, j] = float.Parse(G[j]);
                }
            }
        }

        public void KhoitaoVatCan()
        {
            for (int i = 0; i < _nVatCan; i++)
            {
                Texture2D[] texture2D = new Texture2D[1];
                switch ((int)maTranVatCan[i, 0])
                {
                    case 0:
                        {
                            texture2D[0] = GlobalSettings.gach;
                            break;
                        }
                    case 1:
                        {
                            texture2D[0] = GlobalSettings.bocthep;
                            break;
                        }
                }
                _vatCan.Add(new MySprite(texture2D, maTranVatCan[i, 1], maTranVatCan[i, 2]));
            }
        }

        public void KhoitaoCanCu()
        {
            for (int i = 0; i < _nCanCu; i++)
            {
                Texture2D[] texture2D = new Texture2D[1];
                texture2D[0] = GlobalSettings.canCu;
                _canCu.Add(new MySprite(texture2D, maTranCanCu[i, 0], maTranCanCu[i, 1]));
            }
        }

        public void DrawVatCan(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for(int i = 0; i < _nVatCan; i++)
            {
                _vatCan[i].Draw(gameTime, spriteBatch, Color.White);
            }
        }

        public void DrawCanCu(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _nCanCu; i++)
            {
                _canCu[i].Draw(gameTime, spriteBatch, Color.White);
            }
                
        }
       

        //remove vật cản trong ma trận
        public void RemoveVatCan(int viTri)
        {
            for (int i = viTri; i < _nVatCan - 1; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    maTranVatCan[i, j] = maTranVatCan[i + 1, j];
                }
            }
        }
            
    }
}
